define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Group_agent, $ionicSlideBoxDelegate, $ionicPopover, $stateParams, GoDialog) {
		$scope.id = $stateParams.id;
		$scope.scope = new Object();
		var goods_rule = new Array();
		$scope.goods_rule = new Array();
		$scope.choose_store = new Array();
		$scope.is_all = false;
		var goods = new Array();
		var region = new Array();
		$scope.show_store = new Array();
		$scope.show_region = new Array();
		$scope.region = new Array();
		$scope.scope.choose = '';
		$scope.scope.choose_select = { region_id: 0, region_name: "请选择区域" };
		$scope.store = new Array();
		//滑动时候触发
		$scope.selectIndex = 0;
		$scope.doSelectRegion = function () {
			var list = new Array();
			$scope.dealer.region.forEach(obj => {
				if (obj.id == undefined)
					obj.id = obj.region_id;
				if (obj.name == undefined)
					obj.name = obj.region_name;
			});
			//id是值 name是显示
			GoDialog.showSelect("区域", $scope.dealer.region, $scope.scope.choose_select, function (data) {
				$scope.scope.choose = data.region_name;
				$scope.scope.choose_select = data;
			});
		}
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};

		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
		};

		//点击团购，显示团购的详情
		$scope.detailContent = new Array();
		$scope.init_detailContent = function(goods) {
			for(var i = 0; i < goods.length; i++) {
				var n = $scope.detailContent.length;
				var id = goods[i].goods_id;
				$scope.detailContent[id] = {
					"checked": false,
					"id": id
				}
			}
		}
		$scope.detailChanged = function(index) {
			for(var o in $scope.detailContent) {
				if($scope.detailContent[o].id == index) {
					if($scope.detailContent[o].checked == true)
						$scope.detailContent[o].checked = false;
					else
						$scope.detailContent[o].checked = true;
				} else
					$scope.detailContent[o].checked = false;
			}
		}
		$scope.get_region = function() {
			$scope.get_store();
			if($scope.scope.choose) {
				if($scope.choose_store.length > 0) {
					for(var i = 0; i < $scope.dealer.region.length; i++) {
						if($scope.dealer.region[i].region_name == $scope.scope.choose) {
							for(var j = 0; j < $scope.region.length; j++) {
								if($scope.dealer.region[i].region_id == $scope.region[j].region_id) {
									$scope.region[i] = {
										"region_id": $scope.dealer.region[i].region_id,
										"is_all": $scope.scope.is_all,
										"store": $scope.choose_store
									}
									$scope.show_region[i] = {
										"region_name": $scope.dealer.region[i].region_name,
										"store": $scope.show_store
									}
									$scope.choose_store = new Array();
									$scope.show_store = new Array();
									return;
								}

							}
							var n = $scope.region.length;
							$scope.region[n] = {
								"region_id": $scope.dealer.region[i].region_id,
								"is_all": $scope.scope.is_all,
								"store": $scope.choose_store
							};
							$scope.show_region[n] = {
								"region_name": $scope.dealer.region[i].region_name,
								"store": $scope.show_store
							}
							$scope.choose_store = new Array();
							$scope.show_store = new Array();

						}
					}
				} else
					GoDialog.toast("请选择代理商");
			} else
				GoDialog.toast("请选择地域");
			$scope.choose_store = new Array();
			region = $scope.region;
		}
		$scope.delete_store = function(region_name,index){
			for(var i=0;i<$scope.show_region.length;i++){
				if($scope.show_region[i].region_name==region_name){
					$scope.region[i].store.splice(index,1);
			       $scope.show_region[i].store.splice(index,1);
				}
			}
			
		}
		$scope.delete_region = function(index){
			$scope.show_region.splice(index,1);
			region.splice(index,1);
		}
		$scope.get_store = function() {
			for(var i = 0; i < $scope.store.length; i++) {
				if($scope.store[i].checked == true) {
					var n = $scope.choose_store.length;
					$scope.choose_store[n] = {
						"store_id": $scope.store[i].store_id
					};
					$scope.show_store[n] = {
						"store_name": $scope.store[i].store_name
					}
				}

			}
			//			console.log($scope.choose_store);
		}
		$scope.select_all = function(all) {
			if(all == true) {
				$scope.scope.is_all = true;
				for(var i = 0; i < $scope.store.length; i++) {
					$scope.store[i].checked = true;
				}
			} else {
				$scope.scope.is_all = false;
				for(var i = 0; i < $scope.store.length; i++) {
					$scope.store[i].checked = false;
				}
			}

			console.log($scope.store);
		}
		$scope.checked = function(id) {
			console.log(id);
			for(var i = 0; i < $scope.store.length; i++) {
				if($scope.store[i].store_id == id) {
					if($scope.store[i].checked == false) {
						$scope.scope.is_all = false;
						
						break;
					} else {
						if($scope.store.length == 1) {
							$scope.scope.is_all = true;
							
						}

					}
				} else if($scope.store[i].checked == true) {
					$scope.scope.is_all = true;
					

				} else {
					
					$scope.scope.is_all = false;
					break;
				}
			}
			//			console.log()
			console.log($scope.dealer);
		}
		$scope.get_third = function() {
			$scope.get_region();
			console.log(region);
		}
		$scope.add_goods_rule = function(id) {
			for(var i = 0; i < $scope.dealer.goods.length; i++) {
				if($scope.dealer.goods[i].goods_id == id) {
					var n = $scope.dealer.goods[i].groupbuy_rule.length;
					if($scope.dealer.goods[i].groupbuy_rule[n - 1].group_count && $scope.dealer.goods[i].groupbuy_rule[n - 1].days && $scope.dealer.goods[i].groupbuy_rule[n - 1].promotion_price)
						$scope.dealer.goods[i].groupbuy_rule[n] = {
							"id": '',
							"group_count": "",
							"days": '',
							"promotion_price": ""
						}

					break;
				}
			}
			console.log($scope.dealer.goods);
		}
		$scope.get_goods = function() {
			goods = new Array();
			if($scope.dealer.goods == undefined) {
				GoDialog.toast("请选择活动商品");
			}else{
				for(var m = 0; m < $scope.dealer.goods.length; m++) {
					if($scope.dealer.goods[m].groupbuy_rule == undefined)
						$scope.dealer.goods[m].groupbuy_rule = new Array();
					if($scope.dealer.goods[m].groupbuy_rule.length < 1) {
						GoDialog.toast("请设定" + $scope.dealer.goods[m].goods_name + "的活动规则");
						return;
					}
				var i = goods.length;
				goods[i] = {
					"goods_id": $scope.dealer.goods[m].goods_id,
					"rule": $scope.dealer.goods[m].groupbuy_rule
				};
			}
			}
		}
		$scope.save_goods_rule = function() {
			for(var i = 0; i < $scope.dealer.goods.length; i++) {
				var n = $scope.dealer.goods[i].groupbuy_rule.length;
				if(!$scope.dealer.goods[i].groupbuy_rule[n - 1].group_count || !$scope.dealer.goods[i].groupbuy_rule[n - 1].days || !$scope.dealer.goods[i].groupbuy_rule[n - 1].promotion_price)
					$scope.dealer.goods[i].groupbuy_rule.splice(n - 1, 1);
			}
		}
		$scope.init_data = function() {
			Group_agent.group_detail($scope.id, function(data) {
				$scope.detail = data;
			});
			Group_agent.group_dealer($scope.id, function(data) {
				$scope.dealer = data;
				$scope.isable = $scope.dealer.change_price;
				$scope.init_detailContent($scope.dealer.goods);
				$scope.$watch('scope.choose', function(newValue, oldValue) {
					for(var i = 0; i < $scope.dealer.region.length; i++) {
						if($scope.dealer.region[i].region_name == newValue){
							$scope.store = $scope.dealer.region[i].store;
							$scope.select_all($scope.dealer.region[i].is_all);
						}
					}
				})
			});

		}
		$scope.submit = function() {
			$scope.get_goods();
			//			$scope.get_region();
			if(region.length < 1){
				GoDialog.toast("请添加代理商信息");
				return;
			}
				for(var i=0;i<region.length;i++){
				if(region[i].store==undefined){
					GoDialog.toast("请输入完整的地域信息");
				return;
				}else if($scope.region[i].store.length<1){
					GoDialog.toast("请输入完整的地域信息");
				return;
				}
			}
				Group_agent.group_agenterjion($scope.id, region, goods,$scope.dealer.mail_free, function(data) {
					if(data.success == true) {
						GoDialog.toast("保存成功")
					} else
						GoDialog.toast("保存失败")
				})
			goods = new Array();
		}
		$scope.init_data();
	}

	controller.$inject = ['$scope', 'Group_agent', '$ionicSlideBoxDelegate', '$ionicPopover', '$stateParams', 'GoDialog'];

	return app.registerController("group_agentctrl",controller);
});